# coding:utf-8
# __auth__:felix

import os, sys

# 将项目的源目录加入到环境变量中
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import logging
import time
import logging.config
import yaml
from setting.setting import *


class Logger():

    def __init__(self):
        # 读取配置文件
        with open(os.path.join(LOG_DIR, 'logging.yaml')) as f:
            log_config = yaml.load(f.read(), Loader=yaml.Loader)
            formatters = log_config['formatters']['format']
            level = log_config['filehandlers']['level']
        # 初始化日志收集器
        logger = logging.getLogger("ApiAutoTest")
        # 设置收集器级别
        logger.setLevel(level)
        # 初始化format，设置格式
        fmt = logging.Formatter(formatters)
        # 初始化处理器
        # 设置filehandle
        file = os.path.join(LOG_DIR,'{}.log'.format(time.strftime("%Y%m%d", time.localtime())))
        file_handler = logging.FileHandler(file,encoding="utf-8",mode="a")
        # 设置handler级别
        file_handler.setLevel(level)
        #设置stremhandle
        stream_handler = logging.StreamHandler()
        # 设置handler级别
        stream_handler.setLevel('INFO')

        # 添加handler
        logger.addHandler(file_handler)
        logger.addHandler(stream_handler)
        # 添加日志处理器
        file_handler.setFormatter(fmt)

        self.logger = logger
    def debug(self, msg):
        return self.logger.debug(msg)
    def info(self, msg):
        return self.logger.info(msg)
    def warning(self, msg):
        return self.logger.warning(msg)
    def error(self, msg):
        return self.logger.error(msg)

if __name__ == "__main__":
    Logger().info('abc')